// pages/topicality/index/index.js
//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var sceneData = [];
        if (options.scene != undefined) {
            var scene = decodeURIComponent(options.scene);
            var arrPara = scene.split("/");
            for (var i in arrPara) {
                sceneData.push(arrPara[i])
            }
        }
        this.setData({
            topicId: sceneData[0] ? sceneData[0] : (options.id ? options.id : ''),
            citycode: sceneData[1] ? sceneData[1] : (options.city ? options.city : (ks.getStorageSync("citycode") ? ks.getStorageSync("citycode") : 'sz')),
        })
        ks.setStorageSync('citycode', this.data.citycode)
        util.getUrl(app.globalData.url).then((res) => {
            util.cityList().then((res) => {
                let citydata = res.allcity.filter(v => v.citycode == this.data.citycode);
                this.setData({
                    city: citydata[0].cityname
                })
                ks.setStorageSync('city', this.data.city)
            })
            this.getTopticlist();

        })
    },

    // 子专题列表
    getTopticlist() {
        let data = {
            action: "child",
            pid: this.data.topicId
        };
        util.request('zhuanti.php', data, ).then(res => {
            if (res.data.data) {
                let subList = res.data.data;
                if (subList && subList[0])
                    ks.redirectTo({
                        url: '/pages/topicality/subtopic/subtopic?id=' + subList[0].id + '&city=' + this.data.citycode,
                    })
            } else {
                ks.switchTab({
                    url: "/pages/index/index"
                })
            }
        })
    },
})